//
// Created by jingjia on 10/14/21.
//

#ifndef JINGJIA_JVM_CPP_OSTHREAD_HPP
#define JINGJIA_JVM_CPP_OSTHREAD_HPP

#include "../precompiled/precompiled.hpp"
#include "mutex.hpp"

enum ThreadState {
    ALLOCATED,                    // Memory has been allocated but not initialized
    INITIALIZED,                  // The thread has been initialized but yet started
    RUNNABLE,                     // Has been started and is runnable, but not necessarily running
    MONITOR_WAIT,                 // Waiting on a contended monitor lock
    CONDVAR_WAIT,                 // Waiting on a condition variable
    OBJECT_WAIT,                  // Waiting on an Object.wait() call
    BREAKPOINTED,                 // Suspended at breakpoint
    SLEEPING,                     // Thread.sleep()
    ZOMBIE                        // All done, but not reclaimed yet
};

class OSThread : public CHeapObj<mtThread>{
public:
    OSThread();
    ~OSThread();
    volatile ThreadState _state;
    void setThreadId(pthread_t tid);
    pthread_t threadId() const                   { return _thread_id; }

    static size_t threadIdSize()                  { return sizeof(pthread_t); }

    void setThreadState(ThreadState state) {
        _state = state;
    }
    ThreadState threadState() {
        return _state;
    }
    Monitor* startThreadLock() const;
private:
    pthread_t _thread_id;
    string _name;
    Monitor* monitor;
};


#endif //JINGJIA_JVM_CPP_OSTHREAD_HPP
